#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 1e6 + 10;

int n, k;
int p[N], d[N];

int find(int x){
    if(x != p[x]){
        int t = find(p[x]);
        d[x] += d[p[x]];
        p[x] = t;
    }
    return p[x];
}

int main(){
#ifdef LOCALFLAG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> k;

    for(int i = 1; i <= n; i ++) p[i] = i;
    int res = 0;

    int op, a, b;
    while(k --){
        cin >> op >> a >> b;

        if(a > n || b > n){
            res ++;
            continue;
        }

        if(op == 1){
            int fa = find(a), fb = find(b);
            
            if(fa == fb){
                if((d[a] - d[b]) % 3) res ++;
            }else{
                // fa != fb;
                p[fa] = fb;
                d[fa] = d[b] - d[a];
            }
        }else{
            int fa = find(a), fb = find(b);

            if(fa == fb){ // a -> b
                if((d[a] - d[b] - 1) % 3) res ++;
            }else{
                p[fa] = fb;
                d[fa] = 1 + d[b] - d[a];
            }
        }
    }
    cout << res << '\n';

    return 0;
}